package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.AliveCoinActivateHistory;
import cn.net.shuxing.yifu.common.bean.CoinRatio;
import cn.net.shuxing.yifu.common.bean.UserDealHistory;
import cn.net.shuxing.yifu.common.mapper.CoinRatioMapper;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.shuxing.yifu.common.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CoinRatioService extends BaseService<CoinRatio, CoinRatioMapper> {

    @Autowired
    private AliveCoinActivateHistoryService aliveCoinActivateHistoryService;

    @Autowired
    private UserDealHistoryService userDealHistoryService;

    @Autowired
    private JedisUtil jedisUtil;


    /**
     * 计算涨幅
     *
     * @return
     */
    public BigDecimal calculateCoinGain(Integer coinTypeId) {
        /**
         * 新会员激活数
         * 新激活活币数
         * 在线交易活币数
         */
        List<AliveCoinActivateHistory> activateHistoryList = aliveCoinActivateHistoryService.getsYestodayDayByCoinTypeWithoutComplete(coinTypeId);

        Integer newActivateMemberCount = activateHistoryList.size();

        BigDecimal newActivateAliveCoinCount = new BigDecimal(0);
        for (AliveCoinActivateHistory aliveCoinActivateHistory : activateHistoryList) {
            newActivateAliveCoinCount = newActivateAliveCoinCount.add(aliveCoinActivateHistory.getCount());
        }

        List<UserDealHistory> dealHistoryList = userDealHistoryService.getsYestodayByCoinCount(coinTypeId);
        BigDecimal dealAliveCoinCount = new BigDecimal(0);
        for (UserDealHistory dealHistory : dealHistoryList) {
            dealAliveCoinCount = dealAliveCoinCount.add(dealHistory.getDealAliveCoinCount());
        }

        BigDecimal valueOfRatioA = getValueOfRatioA(coinTypeId, newActivateMemberCount);
        BigDecimal valueOfRatioB = getValueOfRatioB(coinTypeId, newActivateAliveCoinCount);
        BigDecimal valueOfRatioC = getValueOfRatioC(coinTypeId, dealAliveCoinCount);
        BigDecimal valueOfRatioD = getValueOfRatioD(coinTypeId);

        BigDecimal gain = valueOfRatioA.add(valueOfRatioB).add(valueOfRatioC).add(valueOfRatioD);
        return gain;
    }

    public BigDecimal getValueOfRatioA(Integer coinTypeId, Integer count) {
        BigDecimal value = ((CoinRatioMapper) mapper).getValue("a", coinTypeId, new BigDecimal(count));
        if (value == null)
            value = new BigDecimal(0);
        return value;
    }

    public BigDecimal getValueOfRatioB(Integer coinTypeId, BigDecimal count) {
        BigDecimal value = ((CoinRatioMapper) mapper).getValue("b", coinTypeId, count);
        if (value == null)
            value = new BigDecimal(0);
        return value;
    }

    public BigDecimal getValueOfRatioC(Integer coinTypeId, BigDecimal count) {
        BigDecimal value = ((CoinRatioMapper) mapper).getValue("c", coinTypeId, count);
        if (value == null)
            value = new BigDecimal(0);
        return value;
    }

    public BigDecimal getValueOfRatioD(Integer coinTypeId) {
        //Jedis //jedis = jedisUtil.getJedis();
        String gainStr = jedisUtil.get(RedisKeyCreator.CoinRatioD(coinTypeId));
        if (!StringUtils.hasText(gainStr))
        {
            gainStr = "0";
            jedisUtil.set(RedisKeyCreator.CoinRatioD(coinTypeId),gainStr);
        }
        BigDecimal value = new BigDecimal(gainStr);
        //jedisUtil.close();
        return value;
    }

    public int insertRatioA(Integer coinTypeId, Integer low, Integer high, BigDecimal value) {
        CoinRatio coinRatio = new CoinRatio();
        coinRatio.setCoinTypeId(coinTypeId);
        coinRatio.setLow(new BigDecimal(low));
        coinRatio.setHigh(new BigDecimal(high));
        coinRatio.setValue(value);
        coinRatio.setName("a");
        insert(coinRatio);
        return coinRatio.getId();
    }

    public int insertRatioB(Integer coinTypeId, BigDecimal low, BigDecimal high, BigDecimal value) {
        CoinRatio coinRatio = new CoinRatio();
        coinRatio.setCoinTypeId(coinTypeId);
        coinRatio.setLow(low);
        coinRatio.setHigh(high);
        coinRatio.setValue(value);
        coinRatio.setName("b");
        insert(coinRatio);
        return coinRatio.getId();
    }

    public int insertRatioC(Integer coinTypeId, BigDecimal low, BigDecimal high, BigDecimal value) {
        CoinRatio coinRatio = new CoinRatio();
        coinRatio.setCoinTypeId(coinTypeId);
        coinRatio.setLow(low);
        coinRatio.setHigh(high);
        coinRatio.setValue(value);
        coinRatio.setName("C");
        insert(coinRatio);
        return coinRatio.getId();
    }

    public void insertRatioD(Integer coinTypeId, BigDecimal value) {
        //Jedis //jedis = jedisUtil.getJedis();
        jedisUtil.set(RedisKeyCreator.CoinRatioD(coinTypeId), value.toString());
        //jedisUtil.close();
    }


}